package com.system.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.entity.CollectInfo;
import com.system.service.CollectServiceImpl;
import com.system.service.ICollectService;

public class AddCollectInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public AddCollectInfoServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		
		String foodNum = request.getParameter("id");
		String foodName = request.getParameter("name");
		String collect1 = request.getParameter("collect1");
		String collect2 = request.getParameter("collect2");
		String collect3 = request.getParameter("collect3");
		String collect4 = request.getParameter("collect4");
		String collect5 = request.getParameter("collect5");
		
		CollectInfo collectInfo = new CollectInfo(foodNum, foodName, String.valueOf(System.currentTimeMillis()));
		String[] collect = {collect1,collect2,collect3,collect4,collect5};
		int collectTotal = 0;
		for (String string : collect) {
			if (string != "") {
				++collectTotal;
			}
		}
		collectInfo.setAllCollect(collect);
		collectInfo.setCollectTotal(collectTotal);
		ICollectService collectService = new CollectServiceImpl();
		PrintWriter out = response.getWriter();
		if (collectService.addCollectInfo(collectInfo) == "1") {
			out.print("{\"code\":\"1\"}");
		}else {
			out.print("{\"code\":\"0\"}");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
